package com.lz.football.module.friend
{
	import com.common.utils.Reflection;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.FriendEvent;
	import com.liangzhong.model.AppData;
	import com.lz.football.module.friend.item.FriendGrantItem;
	
	import common.BaseView.BaseView;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	
	import lz.controls.Alert;

	/**
	 *好友推荐面板 
	 * @author Administrator
	 * 
	 */	
	public class FriendRecommendView extends BaseView
	{
		private var _vecItem:Vector.<FriendGrantItem>;
		/**
		 * 
		 */
		private var listContain:MovieClip;
		/**
		 *换一组列表 
		 */
		private var turnPage:SimpleButton;
		/**
		 *一键添加  
		 */
		private var addBtn:SimpleButton;
		public function FriendRecommendView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.football.module.friend.recommendView");
			this.addChild(symbolSkin);
			_windowBg = symbolSkin.windowBg;
			isDrag = true;
			setCloseBtn(symbolSkin.closeBtn);
			listContain = symbolSkin.listContain;
			_viewWidth = this.width;
			_viewHeight = this.height;
			turnPage = symbolSkin.turnPage;
			addBtn = symbolSkin.addBtn;
			initItem();
			addEvtListener();
		}
		private function initItem():void
		{
			_vecItem = new Vector.<FriendGrantItem>();
			var item:FriendGrantItem;
			for(var i:int = 0;i < 10;i++)
			{
				item = new FriendGrantItem();
				item.initUI();
				item.y = (item.height + 2) * i;
				item.type = 2;
				listContain.addChild(item);
				_vecItem.push(item);
			}
		}
		override public function addEvtListener():void
		{
			turnPage.addEventListener(MouseEvent.CLICK,onturnPangeHandler);
			addBtn.addEventListener(MouseEvent.CLICK,onAddhandler);
		}
		override public function removeEvtListener():void
		{
			turnPage.removeEventListener(MouseEvent.CLICK,onturnPangeHandler);
			addBtn.removeEventListener(MouseEvent.CLICK,onAddhandler);
		}
		/**
		 *一键添加好友 （全部邀请为好友） 
		 * @param event
		 * 
		 */
		protected function onAddhandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			var arr:Array = [];
			var len:int = _vecItem.length;
			var item:FriendGrantItem;

			for(var i:int=0;i < len;i++)
			{
				item = _vecItem[i];
				if(!item.invitestatus && item.data != null)
				{
					arr.push(item.data.friendid);
					item.invitestatus = true;
				}
			}
			if(arr.length ==0)
			{
				Alert.showErrorTip("已经添加，请换一组");
				return;
			}
			AppEventDispatcher.getInstance().dispatchEvent(new FriendEvent(FriendEvent.INVITE_FRIENDS,{array:arr}));
		}
		
		/**
		 *换一组列表 
		 * @param event
		 * 
		 */
		protected function onturnPangeHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			removeEvtListener();
			AppEventDispatcher.getInstance().dispatchEvent(new FriendEvent(FriendEvent.RECOMMEND_LIST));
		}
		/**
		 *更新列表 
		 * 
		 */
		public function updataList():void
		{
			resetItem();
			var arr:Array = AppData.getInstance().friendManager.recommendList;
			if(arr == null)return;
			var len:int = arr.length > _vecItem.length? _vecItem.length:arr.length;
			var item:FriendGrantItem;
			for(var i:int = 0;i < len;i++)
			{
				_vecItem[i].data = arr[i];
				_vecItem[i].invitestatus = false;
			}
			addEvtListener();
		}
		/**
		 *重置item 
		 * 
		 */
		private function resetItem():void
		{
			var len:int  =_vecItem.length; 
			var item:FriendGrantItem;
			for each(item in _vecItem)
			{
				item.reset();
			}
		}
//		override public function removeEvtListener():void
//		{
//			super.removeEvtListener();
//		}
		override public function destroyView():void
		{
			removeEvtListener();
			while(_vecItem.length)
			{
				_vecItem.pop().destroyView();
			}
			super.destroyView();
		}
	}
}